package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.battle.object.spawner.BattleSpawner;
import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.editor.propertyframe.WorldStaticObjectPropertyFrame;
import se.webbzon.oschi01.worldobject.WorldObject;

public class BattleSpawnerPropertyFrameFactory implements PropertyFrameFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return BattleSpawner.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		return new PropertyFrameImpl(model,(BattleSpawner) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldStaticObjectPropertyFrame {

		// The input fields for this property frame
		private final JTextField factionIdInput, groupIdInput, orderInput;
		
		// The check boxes for this property frame
		private final JCheckBox faceLeftInput;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(EditorModel model, BattleSpawner instance) {
			super(model, instance);
			
			JPanel spawnerPanel = new JPanel();
			spawnerPanel.setLayout(new GridLayout(3,3));
			spawnerPanel.setBorder(BorderFactory.createTitledBorder("Spawner Data:"));
			spawnerPanel.add(new JLabel("Faction id:"));
			spawnerPanel.add(new JLabel("Group id:"));
			spawnerPanel.add(new JLabel("Order:"));
			factionIdInput = new JTextField("" + instance.getFactionId());
			groupIdInput = new JTextField("" + instance.getGroupId());
			orderInput = new JTextField("" + instance.getOrder());
			spawnerPanel.add(factionIdInput);
			spawnerPanel.add(groupIdInput);
			spawnerPanel.add(orderInput);
			faceLeftInput = new JCheckBox("Facing left");
			faceLeftInput.setSelected(instance.isFacingLeft());
			spawnerPanel.add(faceLeftInput);
			
			addPanel("Spawner", spawnerPanel);
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/
		
		@Override public boolean apply(Collection<String> errorMessages) {
			if (super.apply(errorMessages)) try {
				BattleSpawner instance = (BattleSpawner) getInstance();
				instance.setFactionId(Integer.parseInt(factionIdInput.getText()));
				instance.setGroupId(Integer.parseInt(groupIdInput.getText()));
				instance.setOrder(Integer.parseInt(orderInput.getText()));
				instance.setFacingLeft(faceLeftInput.isSelected());
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			} else
				return false;
		}
		
	}

}
